package com.qububu.db.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.db.dao.MessageInfoDao;
import com.qububu.share.reply.dto.MessageInfoDto;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

/**
 * @功能 与文章回复数据有关的数据库操作
 * @author LH
 * */
@Repository
public class MessageInfoDaoImpl implements MessageInfoDao{

	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	/**
	 * @功能 添加文章的评论
	 * @param MessageInfoDto 评论信息
	 */
	public void doAddMessage(MessageInfoDto messageInfo) {
		//解密
		String parentMsgId = messageInfo.getParentMsgId();
//		parentMsgId = SystemUtil.decrypt(parentMsgId);
		String articleId = messageInfo.getArticleId();
//		articleId = SystemUtil.decrypt(articleId);
		
		String sqlToArticleInfo2 = "insert into article_detail_msg(msg_title, msg_content, msg_create_name, msg_create_ip, msg_create_dttm, msg_summary_pic_url, msg_type, article_id, parent_msg_id, user_id) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		Object[] articleDetailInfoValues = {"评论",
						messageInfo.getMsgText(),
						messageInfo.getMsgCreateName(),
						messageInfo.getMsgCreateIp(),
						messageInfo.getMsgCreateDttm(),
						messageInfo.getMsgSummaryPicUrl(),
						null,
						articleId,
						parentMsgId,
						messageInfo.getUserId()
						};
		
		jdbcTemplate.update(sqlToArticleInfo2, articleDetailInfoValues);
	}
	
	/**
	 * @功能 查询评论列表
	 * @author LH
	 * @param messageInfo 文章信息
	 * @return messageList 文章的列表
	 * */
	public ArrayList<MessageInfoDto> doQueryMessage(String articleId) {
		//解密
		articleId = SystemUtil.decrypt(articleId);
		
		ArrayList<MessageInfoDto> messageList = new ArrayList<MessageInfoDto>();
		String sqlToArticleInfo = "select * from article_detail_msg where article_id=? order by msg_create_dttm desc";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sqlToArticleInfo, articleId);
		MessageInfoDto messageInfo = null;
		for (Map<String, Object> elem : list){
			messageInfo = new MessageInfoDto((Long) elem.get("msg_id"), (Long) elem.get("user_id"),(Long) elem.get("parent_msg_id"), (Long) elem.get("article_id"));
			
			messageInfo.setMsgText(String.valueOf(elem.get("msg_content")));
			messageInfo.setMsgCreateName(String.valueOf(elem.get("msg_create_name")));
			messageInfo.setMsgCreateIp(String.valueOf(elem.get("msg_create_ip")));
			messageInfo.setMsgCreateDttm(ParamConverter.convertToInteger(String.valueOf(elem.get("msg_create_dttm"))));
			messageInfo.setMsgSummaryPicUrl(String.valueOf("msg_summary_pic_url"));
			
			messageList.add(messageInfo);
		}
		return messageList;
	}
	
	/**
	 * @功能 删除对文章的评论
	 * @author LH
	 * @param articleId
	 * @param msgId
	 */
	public boolean doDeleteMessage(String parentMsgId, String msgId) {
		//解密
//		parentMsgId = SystemUtil.decrypt(parentMsgId);
//		msgId = SystemUtil.decrypt(msgId);
		
		String sql = "delete from article_detail_msg where parent_msg_id=? and msg_id=?";
		Object[] values = {parentMsgId, msgId};
		
		jdbcTemplate.update(sql, values);
		
		return true;
	}
}
